/*---------------------------------------------------------
    ev_core.lua - Core Gamemode Function
	by Lead4u
---------------------------------------------------------*/

	if not sql.TableExists( "evorp" ) then    
		sql.Query( "CREATE TABLE IF NOT EXISTS evorp ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, player INTEGER NOT NULL, money Currency NOT NULL, savings Currency NOT NULL, lastvisit INTEGER NOT NULL );" )
		sql.Query( "CREATE TABLE IF NOT EXISTS evorp_bonds ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, player INTEGER NOT NULL, amount Currency NOT NULL, expires INTEGER NOT NULL );" ) 
		sql.Query( "CREATE INDEX IDX_EVORP_PLAYER ON evorp ( player DESC );" )
		sql.Query( "CREATE INDEX IDX_EVORP_BOND_PLAYER ON evorp_bonds ( player DESC );" )
		print(" === Creating SQL Tables for EVORP === ")
	end

/*---------------------------------------------------------
	Save Money
		Args: ( player )
---------------------------------------------------------*/

	function evorp:SaveMoney( player )
		evorp.sql:Update( "evorp", "money", evorp.players[player:UniqueID()].money , player )
	end
			
/*---------------------------------------------------------
    AddMoney Function
---------------------------------------------------------*/

	function evorp:AddMoney( player, amount )
		evorp.players[player:UniqueID()].money = evorp.players[player:UniqueID()].money  + amount
		evorp:SaveMoney( player )
	end

/*---------------------------------------------------------
    Payroll Function
---------------------------------------------------------*/

	function evorp.Payroll()
		for k,v in pairs( player.GetAll() ) do
			local class = evorp.Class:ClassFindByPlayer( v )
			print(" === PAYDAY === ")
			evorp:AddMoney( v , evorp.Class.Classes[class].Salary )
			v:ChatPrint("Payday! $" .. evorp.Class.Classes[class].Salary .. " gained.")
			v:ChatPrint("Total: $" .. evorp.players[v:UniqueID()].money) 
		end
	end
	timer.Create("ev_PayrollTimer",60,0, evorp.Payroll, evorp)
	
/*---------------------------------------------------------
    Assign Class on Respawn
---------------------------------------------------------*/	
	
	function evorp.AssignClass( player )
		if ( !evorp.players[player:UniqueID()] ) then
		
				evorp.Class:Join( player, "Citizen" )
		end
	end
	
	--hook.Add( "PlayerSpawn", "evorp_assignclass", evorp.AssignClass, evorp ) 
	
/*---------------------------------------------------------
    Disallow Custom Player Model Choices
---------------------------------------------------------*/	
	
	function evorp.AssignClass( player )
		return false;
	end
	
	hook.Add( "PlayerSetModel", "evorp_nomodel", evorp.AssignClass, evorp ) 
	
/*---------------------------------------------------------
    Load Player Crap on Join
---------------------------------------------------------*/

	function evorp.LoadInformation( player )
		
		local uid = player:UniqueID()
		local data = evorp.sql:Select( "evorp", "money", player ) 
		local bonds = evorp.sql:Select( "evorp_bonds", "*", player ) 
		
		if not data then
			evorp.players[uid] = {};
			evorp.players[uid].money = 500;
			evorp.players[uid].lastvisit = os.time();
			sql.Query( "INSERT into evorp ( player, money, lastvisit ) VALUES ( " .. uid .. ", 500, " .. evorp.players[uid].lastvisit .. " );" )
			print(" === Inserting SQL Record for New Player === ")
		else
			evorp.players[uid] = {};
			evorp.players[uid].money = data.money;
			evorp.players[uid].lastvisit = data.lastvisit;
			print(" === Reading SQL Record for Previous Player === ")
		end
		
			evorp.Class:Join( player, "Citizen" )
		
	end
	
	hook.Add( "PlayerInitialSpawn", "evorp_loadinfo", evorp.LoadInformation, evorp )
	
/*---------------------------------------------------------
    Release Player Data on Exit
---------------------------------------------------------*/	
	
	function evorp.ReleaseData( player )
		
		local uid = player:UniqueID()
		evorp.players[uid] = nil;
			
	end
	
	hook.Add( "PlayerDisconnected", "evorp_releaseinfo", evorp.ReleaseData, evorp )